#Slider for wxSolutionDetails that allows a non-integer scale to be used and places
#tick marks at customised 'change points'.

use Wx 0.15 qw[:allclasses];
use strict;
package wxBudgetSlider;
use Wx qw[:everything];

our @ISA = ("Wx::Panel");
use strict;

sub new {
    my( $self, $parent, $id, $pos, $size, $style, $name ) = @_;

    #Set default arguments
    $parent = undef                 unless defined $parent;
    $id     = -1                    unless defined $id;
    $pos    = wxDefaultPosition     unless defined $pos;
    $size   = wxDefaultSize         unless defined $size;
    $name   = ""                    unless defined $name;
    $style = wxDEFAULT_FRAME_STYLE  unless defined $style;

    #If the panel is resized a full redraw is necessary
    $style = $style | wxFULL_REPAINT_ON_RESIZE;

    #call the constructor of the wxPanel.
    $self = $self->SUPER::new( $parent, $id, $pos, $size, $style, $name );

    #Register the painting event
    Wx::Event::EVT_PAINT($self,  \&OnPaint);

    bless $self, 'wxBudgetSlider';

	#The max value of the slider
	$self->{slider_max} = 1000;

	$self->{slider} = Wx::Slider->new($self, -1, 0, 0, $self->{slider_max}, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL);
    
    $self->{sizer} = Wx::BoxSizer->new(wxHORIZONTAL);
    $self->{sizer}->Add($self->{slider}, 1, wxEXPAND, 0);
	$self->SetSizer($self->{sizer});
	$self->{sizer}->Fit($self);
	$self->Layout();
	$self->OnPaint();
	
	$self->{ticks} = [0,1];	    
    return $self;
}

#Set the tick marks
sub set_ticks {
	my $self = shift;
	if (scalar @_ > 1) {
		$self->{ticks} = \@_;
	} else {
		$self->{ticks} = [0, 1];
	}
	$self->OnPaint();
}

#Set the value
sub set_value {
	my $self = shift;
	my $value = shift;
	unless (defined $value && $value >= $self->{ticks}->[0] && $value <= $self->{ticks}->[-1]) {
		$self->{slider}->SetValue(0);
		return;
	}
	$self->{slider}->SetValue(($value - $self->{ticks}->[0])/$self->{ticks}->[-1]*$self->{slider_max}); 
}

sub get_value {
	my $self = shift;
	return $self->{slider}->GetValue()/$self->{slider_max}*($self->{ticks}->[-1]-$self->{ticks}->[0]);
}

#Return the slider's ID for event registration
sub GetId {
	my $self = shift;
	return $self->{slider}->GetId();
}

sub OnPaint {
	my $self = shift;

	my $drawArea = Wx::PaintDC->new($self);
	
	my $brush = Wx::Pen->new(wxRED, 2, wxSOLID);
	
	my $offset = 19;
	
	$drawArea->SetPen($brush);
	my ($width, $height) = $self->{slider}->GetSizeWH();
	
	foreach (@{$self->{ticks}}) {
		my $x = ($_ - $self->{ticks}->[0])/$self->{ticks}->[-1]*($width-2*$offset)+$offset;
		$drawArea->DrawLine($x,0,$x,100);
	}
	
}